import angular from 'angular';
import angularMeteor from 'angular-meteor';
import template from './todoList.html';
import { Meteor } from 'meteor/meteor';
import { Tasks } from '../../api/tasks.js';
class TodosListCtrl {
  constructor($scope) {
    $scope.viewModel(this);

    this.subscribe('tasks1');

    this.hideCompleted = false;

    this.helpers({
      tasks() {
        const selector = {};

        // If hide completed is checked, filter tasks
        if (this.getReactively('hideCompleted')) {
          selector.checked = {
            $ne: true
          };
        }
        console.log(Tasks.find({}))
        // return Tasks.find({});
        // Show newest tasks at the top
        return Tasks.find(selector, {
          sort: {
            createdAt: -1
          }
        });
      },
      incompleteCount() {
       return Tasks.find({
         checked: {
           $ne: true
         }
       }).count();
     },
     currentUser() {
       console.log(Meteor.user())
        return Meteor.user();
      }
    })
  }

  addTask(newTask) {
    // Insert a task into the collection
    // Tasks.insert({
    //   text: newTask,
    //   createdAt: new Date,
    //   owner: Meteor.userId(),
    //   username: Meteor.user().username
    // });
    Meteor.call('tasks.insert', newTask);

    // Clear form
    this.newTask = '';
  }

 setChecked(task) {
   // Set the checked property to the opposite of its current value
  //  Tasks.update(task._id, {
  //    $set: {
  //      checked: !task.checked
  //    },
  //  });
  Meteor.call('tasks.setChecked', task._id, !task.checked);
 }

 removeTask(task) {
  //  Tasks.remove(task._id);
   Meteor.call('tasks.remove', task._id);
 }

 setPrivate(task) {
    Meteor.call('tasks.setPrivate', task._id, !task.private);
  }

}

export default angular.module('todosList', [
  angularMeteor
])
  .component('todoList', {
    templateUrl: 'imports/components/todoList/todoList.html',
    controller: ['$scope', TodosListCtrl]
  });
